package com.jast.business.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jast.business.domain.BigdataYarnKvLine;
import com.jast.business.param.BigdataYarnKvLineParam;
import com.jast.business.param.BigdataZookeeperKvLineParam;
import com.jast.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jast.common.annotation.Log;
import com.jast.common.enums.BusinessType;
import com.jast.business.domain.BigdataZookeeperKvLine;
import com.jast.business.service.IBigdataZookeeperKvLineService;
import com.jast.common.core.controller.BaseController;
import com.jast.common.core.domain.AjaxResult;
import com.jast.common.utils.poi.ExcelUtil;
import com.jast.common.core.page.TableDataInfo;

/**
 * Zookeeper监控指标近3天指标Controller
 * 
 * @author Jast
 * @date 2023-02-16
 */
@Controller
@RequestMapping("/business/zookeeperLine")
public class BigdataZookeeperKvLineController extends BaseController
{
    private String prefix = "business/zookeeperLine";

    @Autowired
    private IBigdataZookeeperKvLineService bigdataZookeeperKvLineService;

    @RequiresPermissions("business:zookeeperLine:view")
    @GetMapping()
    public String zookeeperLine()
    {
        return prefix + "/zookeeperLine";
    }

    /**
     * 查询Zookeeper监控指标近3天指标列表
     */
    @RequiresPermissions("business:zookeeperLine:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BigdataZookeeperKvLine bigdataZookeeperKvLine)
    {
        startPage();
        List<BigdataZookeeperKvLine> list = bigdataZookeeperKvLineService.selectBigdataZookeeperKvLineList(bigdataZookeeperKvLine);
        return getDataTable(list);
    }

    /**
     * 导出Zookeeper监控指标近3天指标列表
     */
    @RequiresPermissions("business:zookeeperLine:export")
    @Log(title = "Zookeeper监控指标近3天指标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BigdataZookeeperKvLine bigdataZookeeperKvLine)
    {
        List<BigdataZookeeperKvLine> list = bigdataZookeeperKvLineService.selectBigdataZookeeperKvLineList(bigdataZookeeperKvLine);
        ExcelUtil<BigdataZookeeperKvLine> util = new ExcelUtil<BigdataZookeeperKvLine>(BigdataZookeeperKvLine.class);
        return util.exportExcel(list, "Zookeeper监控指标近3天指标数据");
    }

    /**
     * 新增Zookeeper监控指标近3天指标
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存Zookeeper监控指标近3天指标
     */
    @RequiresPermissions("business:zookeeperLine:add")
    @Log(title = "Zookeeper监控指标近3天指标", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BigdataZookeeperKvLine bigdataZookeeperKvLine)
    {
        return toAjax(bigdataZookeeperKvLineService.insertBigdataZookeeperKvLine(bigdataZookeeperKvLine));
    }


    /**
     * 更新/新增Zookeeper监控指标近3天指标
     */
    @Log(title = "Zookeeper监控指标近3天指标", businessType = BusinessType.UPDATE)
    @PostMapping("/upsert")
    @ResponseBody
    public AjaxResult upsert(BigdataZookeeperKvLine bigdataZookeeperKvLine)
    {
        return toAjax(bigdataZookeeperKvLineService.upsertBigdataZookeeperKvLine(bigdataZookeeperKvLine));
    }

    /**
     * 修改Zookeeper监控指标近3天指标
     */
    @RequiresPermissions("business:zookeeperLine:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BigdataZookeeperKvLine bigdataZookeeperKvLine = bigdataZookeeperKvLineService.selectBigdataZookeeperKvLineById(id);
        mmap.put("bigdataZookeeperKvLine", bigdataZookeeperKvLine);
        return prefix + "/edit";
    }

    /**
     * 修改保存Zookeeper监控指标近3天指标
     */
    @RequiresPermissions("business:zookeeperLine:edit")
    @Log(title = "Zookeeper监控指标近3天指标", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BigdataZookeeperKvLine bigdataZookeeperKvLine)
    {
        return toAjax(bigdataZookeeperKvLineService.updateBigdataZookeeperKvLine(bigdataZookeeperKvLine));
    }

    /**
     * 删除Zookeeper监控指标近3天指标
     */
    @RequiresPermissions("business:zookeeperLine:remove")
    @Log(title = "Zookeeper监控指标近3天指标", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bigdataZookeeperKvLineService.deleteBigdataZookeeperKvLineByIds(ids));
    }

    /**
     * 查询Hbase监控指标最新值列表
     */
    @PostMapping("/getBasicInfo")
    @ResponseBody
    public AjaxResult getBasicInfo(BigdataZookeeperKvLineParam bigdataZookeeperKvLineParam)
    {
        System.out.println("请求参数:"+ JSON.toJSONString(bigdataZookeeperKvLineParam));

        List<BigdataZookeeperKvLine> bigdataYarnKvLines = bigdataZookeeperKvLineService.selectBigdataZookeeperKvLineList(bigdataZookeeperKvLineParam);
        System.out.println("bsize:"+bigdataYarnKvLines.size());
        JSONObject jsonObject = new JSONObject();
        //类型->机器->值
        for (BigdataZookeeperKvLine bigdataYarnKvLine : bigdataYarnKvLines) {
            String htype = bigdataYarnKvLine.getHtype();
            String hkey = bigdataYarnKvLine.getHkey();
            String hvalue = bigdataYarnKvLine.getHvalue();
            String node = bigdataYarnKvLine.getNode();
            String monitorTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,bigdataYarnKvLine.getMonitorTime());
            String type = htype + hkey;
            if(jsonObject.containsKey(type)){
                JSONObject jsonObject1 = jsonObject.getJSONObject(type);
                if(jsonObject1.containsKey("time")){
                    JSONArray time = jsonObject1.getJSONArray("time");
                    if(!time.contains(monitorTime)) {
                        time.add(monitorTime.toString());
                    }
                }else{
                    JSONArray time = new JSONArray();
                    time.add(monitorTime);
                    jsonObject1.put("time",time);
                }
                if(jsonObject1.containsKey(node)){
                    JSONArray jsonArray = jsonObject1.getJSONArray(node);
                    jsonArray.add(hvalue);
                    jsonObject1.put(node,jsonArray);
                }else{
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(hvalue);
                    jsonObject1.put(node,jsonArray);
                }
                jsonObject.put(type,jsonObject1);
            }else{
                JSONObject jsonObject1 = new JSONObject();
                JSONArray jsonArray = new JSONArray();
                jsonObject1.put(node,jsonArray);
                jsonArray.add(hvalue);
                jsonObject.put(type,jsonObject1);
            }
        }
        System.out.println("请求结果："+jsonObject.toJSONString());
        return success(jsonObject);
    }
}
